package com.example.demo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohei.crm.common.exception.XiaoheiException;
import com.xiaohei.crm.common.result.Result;
import com.xiaohei.crm.dao.Inventory;
import com.xiaohei.crm.dao.Material;
import com.xiaohei.crm.dao.Orders;
import com.xiaohei.crm.mapper.InventoryMapper;
import com.xiaohei.crm.mapper.MaterialMapper;
import com.xiaohei.crm.mapper.OrdersMapper;
import com.xiaohei.crm.mapper.RegionMapper;
import com.xiaohei.crm.service.MaterialService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.xiaohei.crm.common.RedisEnums.MATERIAL_REGISTRY_KEY;
import static com.xiaohei.crm.common.result.ResultCodeEnum.DATA_ERROR;


@Service
@RequiredArgsConstructor
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {
    private final InventoryMapper inventoryMapper;
    private final MaterialMapper materialMapper;
    private final StringRedisTemplate stringRedisTemplate;

    private final RegionMapper regionMapper;

    private final OrdersMapper ordersMapper;

    @Override
    public Result<IPage> findByPage(Integer pageIndex, Integer pageSize, String equipmentName, String equipmentType) {
        Page<Material> page = new Page<>();
        LambdaQueryWrapper<Material> eq = Wrappers.lambdaQuery(Material.class).eq(Material::getDelFlag, 0);

        if (!StrUtil.isEmpty(equipmentName)) {
            eq.like(Material::getEquipmentName, equipmentName);
        }
        if (!StrUtil.isEmpty(equipmentType)) {
            eq.like(Material::getEquipmentType, equipmentType);
        }
        Page<Material> materialPage = materialMapper.selectPage(page, eq);
        return Result.ok(materialPage);
    }

    @Override
    public void saveMaterial(Material material) {
        String s = stringRedisTemplate.opsForValue().get(MATERIAL_REGISTRY_KEY + material.getId());
        if (!StrUtil.isEmpty(s)) {
            throw new XiaoheiException(DATA_ERROR);
        }

        int isInsert = baseMapper.insert(material);
        if (isInsert < 1) {
            throw new XiaoheiException(DATA_ERROR);
        }

        stringRedisTemplate.opsForValue().set(MATERIAL_REGISTRY_KEY + material.getId(),material.getId() + material.getEquipmentName());

    }

    @Override
    public void updateMaterialById(Material material) {
        String s = stringRedisTemplate.opsForValue().get(MATERIAL_REGISTRY_KEY + material.getId());
        if (StrUtil.isEmpty(s)) {
            throw new XiaoheiException(DATA_ERROR);
        }

        LambdaQueryWrapper<Material> eq =
                Wrappers.lambdaQuery(Material.class)
                        .eq(Material::getId, material.getId())
                        .eq(Material::getDelFlag,0);
        baseMapper.update(material, eq);
    }

    @Override
    public void removeByMaterialId(String id) {
        String s = stringRedisTemplate.opsForValue().get(MATERIAL_REGISTRY_KEY + id);
        if (StrUtil.isEmpty(s)) {
            throw new XiaoheiException(DATA_ERROR);
        }

        //绑定的orders
        LambdaQueryWrapper<Orders> eq =
                Wrappers.lambdaQuery(Orders.class)
                        .eq(Orders::getCustomerId, id)
                        .eq(Orders::getDelFlag, 0);
        List<Orders> orders = ordersMapper.selectList(eq);
        if(!CollectionUtil.isEmpty(orders)){
            throw new XiaoheiException(DATA_ERROR);
        }

        //检查Inventory设备表
        LambdaQueryWrapper<Inventory> eq1 = Wrappers.lambdaQuery(Inventory.class)
                .eq(Inventory::getMaterialId, id)
                .eq(Inventory::getDelFlag, 0);
        Inventory inventoryDb = inventoryMapper.selectOne(eq1);
        if(inventoryDb != null && !inventoryDb.getMaterialNumber().equals(0)){
            throw new XiaoheiException(DATA_ERROR);
        }

        LambdaUpdateWrapper<Material> updateWrapper =
                Wrappers.lambdaUpdate(Material.class).eq(Material::getId, id);
        Material material = Material.builder().delFlag(1).build();
        int update = baseMapper.update(material, updateWrapper);
        if(update < 0){
            throw new XiaoheiException(DATA_ERROR);
        }
        stringRedisTemplate.delete(MATERIAL_REGISTRY_KEY + id);
    }

}
